package com.ellricky.newtv

import android.content.pm.ActivityInfo
import android.view.KeyEvent
import androidx.fragment.app.Fragment
import com.andr.common.ui.activity.BaseActivity
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.util.ActivityUtil
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.entity.newtv.ClassInfo
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewEntityCallback
import com.ellricky.newtv.bean.TvCourseDetail
import com.ellricky.newtv.const.TvConst
import com.ellricky.newtv.utils.CacheToken
import kotlinx.android.synthetic.main.activity_new_tv_class_manage.*
import kotlinx.android.synthetic.main.activity_new_tv_class_manage.tv_title
import kotlinx.android.synthetic.main.activity_new_tv_preview.*

class NewTvFileDetailActivity : BaseActivity<Void, BasePresenter<Void>>() {

    private val type: String by lazy {
        intent?.getStringExtra(TvConst.KEY_TYPE) ?: ""
    }

    private val id: String by lazy {
        intent?.getStringExtra(TvConst.KEY_ID) ?: ""
    }

    private val classId: String by lazy {
        intent?.getStringExtra(TvConst.KEY_CLASS_ID) ?: ""
    }

    private val fileList: ArrayList<String> by lazy {
        intent?.getStringArrayListExtra(TvConst.KEY_IMAGE) ?: arrayListOf()
    }

    private val classInfo: ClassInfo? by lazy {
        LocalCacheUtil.readTvClassInfo()
    }

    private var fragment: Fragment? = null

    override fun setOrientation() {
        this.requestedOrientation =
            ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
    }

    override fun getContentLayout(): Int {
        return R.layout.activity_new_tv_file_detail
    }

    override fun initUIView() {
        CacheToken.verifyToken()
        if (type.isEmpty()) {
            finish()
            return
        }
        if (type == "imgs") {
            loadImageFragment()
        } else {
            loadVideoFragment()
        }
        addRecord()
    }

    /**
     * 主要是为了添加记录
     */
    private fun addRecord() {
        val url = EAppUtil.getTvApiHost()
        val paramMap = mutableMapOf(
            "action" to "wareDetail",
            "ware_id" to id,
            "eid" to LocalCacheUtil.getTvUid()
        )
        if (classId.isNotEmpty()) {
            paramMap["class_id"] = classId
        } else if (classInfo != null) {
            paramMap["class_id"] = classInfo?.id ?: ""
        }
        HttpUtils.getInstance()
            .postFrom(url, paramMap, object : NewEntityCallback<TvCourseDetail>(this, false) {
                override fun onSuccess(file: TvCourseDetail, var2: String?) {

                }
            }, this)
    }

    private fun loadImageFragment() {
        fragment = NewTvImagePreviewFragment.newInstance(fileList)
        supportFragmentManager
            .beginTransaction()
            .replace(R.id.container, fragment!!)
            .commitAllowingStateLoss()
    }

    private fun loadVideoFragment() {
        fragment = NewTvVideoPreviewFragment.newInstance(fileList)
        supportFragmentManager
            .beginTransaction()
            .replace(R.id.container, fragment!!)
            .commitAllowingStateLoss()
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        when (keyCode) {
            KeyEvent.KEYCODE_DPAD_LEFT -> {
                if (fragment is NewTvImagePreviewFragment) {
                    (fragment as NewTvImagePreviewFragment).toLeft()
                }
            }
            KeyEvent.KEYCODE_DPAD_RIGHT -> {
                if (fragment is NewTvImagePreviewFragment) {
                    (fragment as NewTvImagePreviewFragment).toRight()
                }
            }
        }
        return super.onKeyDown(keyCode, event)
    }
}